package studentsinfomanagement.view.teacher;

import studentsinfomanagement.bean.Course;
import studentsinfomanagement.bean.SC;
import studentsinfomanagement.bean.Student;
import studentsinfomanagement.dao.DepartmentDAO;
import studentsinfomanagement.dao.SCDAO;
import studentsinfomanagement.dao.StudentDAO;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

public class SCTeacherView extends JFrame {

    private SCTeacherView scTeacherView = null;
    private Course course = null;
    private SCDAO scdao = null;
    private DefaultTableModel tableModel = null;

    public SCTeacherView(Course course){
        scTeacherView = this;
        this.course = course;
        scdao = SCDAO.getInstance();
        this.setTitle("选修该课程的学生信息概览");//标题
        this.setSize(550,500);//大小
        this.setLocationRelativeTo(null);//居中
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//关闭操作隐藏界面
        this.setResizable(false);//大小不可调节

        BorderLayout bl = new BorderLayout();
        this.setLayout(bl);

        //课程滚动显示区域
        String[] columnNames = null;  // 列名
        String [][]tableVales= null; //数据
        showStudents(columnNames,tableVales);//获取该课程的学生信息信息
        JTable table = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(table);

        //退出查看界面
        JPanel jp_exit = new JPanel();
        JButton btn_exit = new JButton("退出");
        jp_exit.add(btn_exit);

        //将组件添加到界面
        this.add(scrollPane,BorderLayout.CENTER);
        this.add(jp_exit,BorderLayout.SOUTH);

        //界面可见
        this.setVisible(true);


//添加监听器    修改学生成绩
        table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                int sel_row = table.getSelectedRow();
                int sel_column = table.getSelectedColumn();
                if(sel_row >= 0 && sel_column ==4){//过滤非法值（修改学生成绩）
                    String s_grade = JOptionPane.showInputDialog(scTeacherView,"请输入学生成绩");
                    Float grade = Float.parseFloat(s_grade);
                    String sno = (String) table.getValueAt(sel_row,0);
                    SC sc = new SC();
                    sc.setSno(sno);
                    sc.setCno(course.getCno());
                    sc.setGrade(grade);
                    boolean result = scdao.update(sc);
                    if(!result){//修改成绩失败
                        JOptionPane.showMessageDialog(scTeacherView,"修改成绩失败！");
                    }else{//修改成绩成功
                        JOptionPane.showMessageDialog(scTeacherView,"修改成绩成功！");
                        //显示成绩
                        table.setValueAt(s_grade,sel_row,4);
                    }
                }
            }
        });

        btn_exit.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                scTeacherView.setVisible(false);
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });

    }

    /**
     * 获取选修该课程的全部学生信息
     * @param columnNames
     * @param tableVales
     */
    private void showStudents(String[] columnNames, String[][] tableVales) {
        SCDAO scdao = SCDAO.getInstance();
        StudentDAO studentDAO = StudentDAO.getInstance();
        DepartmentDAO departmentDAO = DepartmentDAO.getInstance();
        //选出该课程的全部选课列表
        List<SC> scList = scdao.getStudentsByCno(course.getCno());
        List<Student> studentList = new ArrayList<>();
        //得到选课列表中所有学生信息
        for(SC sc:scList){
            String sno = sc.getSno();
            Student student = studentDAO.getStudentBySno(sno);
            studentList.add(student);
        }

        columnNames = new String[]{"学号","姓名","性别","专业","成绩"};
        tableVales = new String[studentList.size()][];
        int i = 0;
        for(Student student:studentList){
            tableVales[i] = new String[5];
            tableVales[i][0] = student.getSno();
            tableVales[i][1] = student.getName();
            String sex = student.getSex();
            if(sex.equals("F")) {
                sex = "女";
            }else{
                sex = "男";
            }
            tableVales[i][2] = sex;
            String dno = student.getDepartment();
            tableVales[i][3] = departmentDAO.getDepartmentByDno(dno).getName();
            //查看学生该门课程的成绩
            Float grade = scdao.getGrade(student.getSno(),course.getCno());
            tableVales[i][4] = grade.toString();
            i++;
        }

        tableModel = new DefaultTableModel(tableVales,columnNames){
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
    }
}
